package com.hserver.docker.controller;

import cn.hserver.plugin.web.annotation.WebSocket;
import cn.hserver.plugin.web.context.WebConstConfig;
import cn.hserver.plugin.web.handlers.Ws;
import cn.hserver.plugin.web.interfaces.WebSocketHandler;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;

import java.util.HashMap;
import java.util.Map;

/**
 * @author hxm
 */
@WebSocket("/ws")
public class DataWebSocket implements WebSocketHandler {

    public final static Map<String,Ws> wsMap=new HashMap<>();

    @Override
    public void onConnect(Ws ws) {
        System.out.println(ws.getUid());
    }

    @Override
    public void onMessage(Ws ws) {
        String res = ws.getMessage();
        try {
            JsonNode jSonNode= WebConstConfig.OBJECT_MAPPER.readTree(res);
            String type=jSonNode.get("type").asText();
            String recordId=jSonNode.get("record_id").asText();
            if (type!=null&&recordId!=null){
                wsMap.put(recordId,ws);
                return;
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void disConnect(Ws ws) {
        System.out.println(ws.getUid());
        wsMap.remove(ws.getUid());
    }
}
